package com.yishan.controller;

import com.yishan.Dto.StartDto;
import com.yishan.entity.SysOrg;
import com.yishan.entity.SysUser;
import com.yishan.service.FlowService;
import liquibase.pro.packaged.S;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;

@RestController
@RequestMapping("/flow")
public class FlowController {

    @Autowired
    private FlowService flowService;

    /**
     * 创建组
     * @param
     * @return
     */
    @PostMapping("newGroup")
    public String newGroup(@RequestBody SysOrg sysOrg) {
       String groupId = flowService.newGroup(sysOrg);
       return "创建成功，组ID："+groupId;
    }

    @PostMapping("newUser")
    public String newUser(@RequestBody SysUser sysUser) {
        String userId = flowService.newUser(sysUser);
        return "创建成功，用户ID："+userId;
    }
    /**
     * 部署流程
     * @return
     */
    @PostMapping("/deployment")
    public String deployment(@RequestParam("file") File file) throws FileNotFoundException {
        flowService.deployment(file);
        return "success";
    }

    @DeleteMapping("/delete")
    public String delete(String processDefinitionId) {
        flowService.deleteProcessDefinition(processDefinitionId);
        return "success";
    }

    /**
     * 发起流程
     * @param
     * @return
     */
    @PostMapping("/start")
    public String start(@RequestBody StartDto stratDto) {
        Map<String, Object> variables = stratDto.getVariables();
        String processCode = stratDto.getProcessCode();
        String processInstanceId = flowService.startProcess(processCode,variables);
        return "发起流程成功，流程实例ID："+processInstanceId;
    }
}